/*
 * Copyright (C) 2010 Moduad Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cc.telecomdigital.tdstock.activity.groups.more.push.client;

import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * This class is to manage the notificatin service and to load the
 * configuration.
 * 
 * @author hunter
 */
public final class ServiceManager
{

	private static final String	TAG	= ServiceManager.class.getSimpleName();

	private Context			context;

	private SharedPreferences	sharedPrefs;

	//private Properties props;

	//private String version =Constants.version;

	private String				apiKey;

	private String				xmppHost;

	private String				xmppPort;

	private String				callbackActivityPackageName;

	private String				callbackActivityClassName;

	/*	public ServiceManager(Context context) {
			this.context = context;

			// if (context instanceof Activity) {
			// Log.i(TAG, "Callback Activity...");
			// Activity callbackActivity = (Activity) context;
			// callbackActivityPackageName =
			// MoreSettingActivity.this.getPackageName();
			// callbackActivityClassName = MoreSettingActivity.getClass().getName();
			// }
			props = loadProperties();
			apiKey = props.getProperty("apiKey", "");
			xmppHost = props.getProperty("xmppHost", "127.0.0.1");
		    xmppPort = props.getProperty("xmppPort", "5222");
			//Log.i(TAG, "apiKey=" + apiKey);
			//Log.i(TAG, "xmppHost=" + xmppHost);
			//Log.i(TAG, "xmppPort=" + xmppPort);

			sharedPrefs = context.getSharedPreferences(
					Constants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
			Editor editor = sharedPrefs.edit();
			editor.putString(Constants.API_KEY, apiKey);
			editor.putString(Constants.VERSION, version);
			editor.putString(Constants.XMPP_HOST, xmppHost);
			editor.putInt(Constants.XMPP_PORT, Integer.parseInt(xmppPort));
			editor.putString(Constants.CALLBACK_ACTIVITY_PACKAGE_NAME,
					callbackActivityPackageName);
			editor.putString(Constants.CALLBACK_ACTIVITY_CLASS_NAME,
					callbackActivityClassName);
			editor.commit();
			// Log.i(LOGTAG, "sharedPrefs=" + sharedPrefs.toString());
		}*/

	public ServiceManager(Context context)
	{
		this.context = context;

		apiKey = Constants.XMPP_APIKEY;
		//xmppHost = "192.168.199.52";
		//xmppHost="quotedev3.telecomdigital.cc";
		//Data.getDomainAndAccountAndTokenAndDevice_token(context);
		xmppHost = Constants.XMPP_DEFAULT_HOST;
		xmppPort = Constants.XMPP_DEFAULT_PORT;

		sharedPrefs = context.getSharedPreferences(
				Constants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
		Editor editor = sharedPrefs.edit();
		editor.putString(Constants.API_KEY, apiKey);
		editor.putString(Constants.VERSION, Constants.version);
		editor.putString(Constants.XMPP_HOST, xmppHost);
		editor.putInt(Constants.XMPP_PORT, Integer.parseInt(xmppPort));
		editor.putString(Constants.CALLBACK_ACTIVITY_PACKAGE_NAME,
				callbackActivityPackageName);
		editor.putString(Constants.CALLBACK_ACTIVITY_CLASS_NAME,
				callbackActivityClassName);
		editor.commit();
	}

	public void startService(String form)
	{
		TDStockLog.d(TAG, "form to  " + form);
		Thread serviceThread = new Thread(new Runnable()
		{
			@Override
			public void run()
			{
				TDStockLog.d(TAG, "ServiceManager 重启");
				Intent intent = NotificationService.getIntent();
				context.startService(intent);
				// context.bindService(intent, null,
				// Service.START_CONTINUATION_MASK);
			}
		});
		serviceThread.start();
	}

	public void stopService()
	{
		TDStockLog.d(TAG, "stop ......");
		Intent intent = NotificationService.getIntent();
		context.stopService(intent);
	}

	/*private Properties loadProperties() {

			Properties props = new Properties();
			try {
				int id = context.getResources().getIdentifier("androidpn", "raw",
						context.getPackageName());
				props.load(context.getResources().openRawResource(id));
			} catch (Exception e) {
				Log.e(TAG, "Could not find the properties file.", e);
				// e.printStackTrace();
			}
			return props;
		}*/
	public void setNotificationIcon(int iconId)
	{
		Editor editor = sharedPrefs.edit();
		editor.putInt(Constants.NOTIFICATION_ICON, iconId);
		editor.commit();
	}

	public static void getHostInfo()
	{

	}
}
